package com.china08.yxyapi.api.manage.tec.teaching.mistakes;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.entity.db.res.YxyResBookContentsKnoledges;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.repository.mg.mistakes.TeaMistakesQuesRepository;
import com.china08.yxyapi.util.YxyResUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class MistakesBookContentController extends YxyApiBaseController {

    @Autowired
    private YxyResBookContentsRepository yrbcr;

    @Autowired
    private YxyResUtil yxyResUtil;

    @Autowired
    private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;

    @Autowired
    private TeaMistakesQuesRepository teaMistakesQuesRepository;

    @ApiOperation(value = "获得目录")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/mistakes/bookcontent", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();

        String teaId = getProfile(request, response).getId();

        List<YxyResBookContents> listcoContents = yrbcr.findByBookIdAndIsViewOrderByOrderNum(reqModel.book, 1);
        List<BookContentRespModel> list = new ArrayList<MistakesBookContentController.BookContentRespModel>();

        List<String> bookcontentList = new ArrayList<>();

        for (YxyResBookContents yxyResBookContents : listcoContents) {
            BookContentRespModel bookContentRespModel = new BookContentRespModel();
            bookContentRespModel.id = yxyResBookContents.getId();
            bookContentRespModel.name = yxyResBookContents.getName();
            bookContentRespModel.pId = yxyResBookContents.getpId();

            bookcontentList = yxyResUtil.findTocByTocParent(yxyResBookContents.getId());
            List<YxyResBookContentsKnoledges> knowIdLst = yxyResBookContentsKnoledgesRepository
                    .findByBookContentIdIn(bookcontentList);
            List<Integer> know = new ArrayList<>();
            if (knowIdLst.size() > 0) {
                for (YxyResBookContentsKnoledges yrbcK : knowIdLst) {
                    know.add(yrbcK.getKnowledgeId());
                }
            }
            Integer count = teaMistakesQuesRepository.countByPublishUserIdAndKnowIdLstIn(teaId, know);
            bookContentRespModel.flag = count > 0 ? 1 : 0;
            list.add(bookContentRespModel);
        }
        model.bookContent = list;
        model.setMessage("成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String book;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(book, null), "请填写课本ID");
        }

    }

    protected static class BookContentRespModel {
        public String id;
        public String pId;
        public String name;
        public Integer flag;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getpId() {
            return pId;
        }

        public void setpId(String pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getFlag() {
            return flag;
        }

        public void setFlag(Integer flag) {
            this.flag = flag;
        }

    }

    protected static class RespModel extends YxyApiRespModel {

        public List<BookContentRespModel> bookContent;

        public List<BookContentRespModel> getBookContent() {
            return bookContent;
        }

        public void setBookContent(List<BookContentRespModel> bookContent) {
            this.bookContent = bookContent;
        }

    }

}
